package fr.vahren.dotastrat.game.command;

import java.util.List;

import fr.vahren.dotastrat.game.D2SGame;
import fr.vahren.dotastrat.model.D2SAbility;
import fr.vahren.dotastrat.model.D2SHeroUnit;
import fr.vahren.dotastrat.model.D2SMap;
import fr.vahren.dotastrat.model.D2SPlayer;
import fr.vahren.dotastrat.model.D2SUnit;
import fr.vahren.dotastrat.model.D2SZone;

public class D2SCommand {

	protected D2SAbility ability;

	protected D2SPlayer player;

	protected D2SHeroUnit unitSnapshot;

	protected D2SMap map;

	protected List<D2SUnit> targetUnits;
	protected D2SZone targetZone;

	protected D2SHeroUnit unit;

	protected D2SGame game;

	/**
	 * goes from 0 to 4, 4 is highest priority. Commands are resolved from
	 * highest to lowest priority.
	 */
	protected int priority;

	protected int range;

	public D2SCommand(D2SAbility ability, D2SPlayer player, D2SHeroUnit unit,
			D2SMap map, List<D2SUnit> targetUnits, D2SZone targetZone, D2SGame g) {
		super();
		this.ability = ability;
		this.player = player;
		this.unitSnapshot = unit.snapshot();
		this.unit = unit;
		this.map = map;
		this.targetUnits = targetUnits;
		this.targetZone = targetZone;
		game = g;
		// get the priority from the ability or item used
		if (ability != null) {
			priority = ability.getPriority();
			range = ability.getRange();
		}
	}

	public void execute() {
		if (targetUnits != null && !targetUnits.isEmpty()) {
			for (D2SUnit target : targetUnits) {
				if (!map.isRangeOk(unit, target, range)) {
					return;
				}
			}
		} else if (targetZone != null) {
			if (!map.isRangeOk(unit, targetZone, range)) {
				return;
			}
		}
		unit.triggerAbilityListeners(ability);
		ability.execute(player, map, unit, targetUnits, targetZone);

	}

	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

}
